﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Amazon.SimpleDB;
using Amazon.SimpleDB.Model;
using AmzAttribute = Amazon.SimpleDB.Model.Attribute;
using SimpleDbBrowser.ViewModel;

namespace SimpleDbBrowser.View.Views
{
    /// <summary>
    /// Interaction logic for BrowsingView.xaml
    /// </summary>
    public partial class BrowsingView : UserControl
    {
        public BrowsingView()
        {
            this.InitializeComponent();
            this.Loaded += InitializeOnLoaded;
            this.Unloaded += UninitializeOnUnloaded;
        }

        void InitializeOnLoaded(object sender, RoutedEventArgs e)
        {
            this.DataContextChanged += HandleDataContextChanged;

            if (this.DataContext != null) 
                this.HandleDataContextChanged(this, new DependencyPropertyChangedEventArgs(FrameworkElement.DataContextProperty, null, this.DataContext));
        }

        void UninitializeOnUnloaded(object sender, RoutedEventArgs e)
        {
            this.DataContextChanged -= HandleDataContextChanged;
        }

        private void HandleDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var mainVm = this.DataContext as MainViewModel;
            if (mainVm != null)
            {
                var items = mainVm.Items as INotifyCollectionChanged;
                items.CollectionChanged += HandleItemsCollectionChanged;
            }

            var oldVm = e.OldValue as MainViewModel;
            if (oldVm != null)
            {
                var items = oldVm.Items as INotifyCollectionChanged;
                items.CollectionChanged -= HandleItemsCollectionChanged;
            }
        }

        private void HandleItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // If an item is added, we want to make sure columns exist for its attributes.  
            // If an item is removed, we want to clean up old columns that nothing needs anymore.

            // First, get all the columns we already have
            var view = this.ItemsTable.View as GridView;
            if (view == null) 
                return;

            var cols = view.Columns.Select(c => c.Header.ToString()).ToList();
            var required = new List<string>();
            var removable = new List<string>();

            if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace)
            {
                required = e.NewItems.Cast<Item>().SelectMany(i => i.Attribute).Select(attr => attr.Name).Distinct().ToList();
                ////// Find required columns
                ////foreach (Item item in e.NewItems)
                ////{
                ////    foreach (var attr in item.Attribute)
                ////    {
                ////        if (!cols.Contains(attr.Name))
                ////        {
                ////            required.Add(attr.Name);
                ////        }
                ////    }
                ////}
            }

            if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Replace)
            {
                // Find candidates for removal
            }

            
        }
    }
}
